package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.quotation.QuExchangeRate;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class QuExchangeRateServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	public QuExchangeRateServiceImpl(){
	}
	
	@Override
	public String delete(int id) throws Exception {
		return super.delete(QuExchangeRate.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(QuExchangeRate.class.getName(), id);
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(QuExchangeRate.class.getName(), page, sort, order);
	}
	
	public String getAll(String sort, String order) throws Exception{
		List<QuExchangeRate> objList = this.dao.getAll(QuExchangeRate.class.getName(), sort, order);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (QuExchangeRate obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(objList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<QuExchangeRate> objList = this.dao.getAllPagination(QuExchangeRate.class.getName(), offset,
				Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (QuExchangeRate obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(QuExchangeRate.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

}
